package com.superb.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.superb.common.core.annotation.SuperbDataScope;
import com.superb.common.core.annotation.SuperbRestMapping;
import com.superb.common.core.model.Result;
import com.superb.common.database.vo.BatchId;
import com.superb.common.redis.key.KeyType;
import com.superb.common.redis.key.RedisKey;
import com.superb.common.redis.utils.RedisUtils;
import com.superb.common.utils.HeardersUtils;
import com.superb.system.api.entity.Organization;
import com.superb.system.service.SystemOrganizationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * 部门机构;(system_organization)表控制层
 * @Author: ajie
 * @CreateTime: 2024-6-14
 */
@SuperbDataScope
@Tag(name = "部门机构接口")
@SuperbRestMapping("/organization")
@ApiSupport(author = "阿杰：ajie20999@163.com", order = 6)
public class SystemOrganizationController {

    @Resource
    private SystemOrganizationService organizationService;

    @GetMapping("tree")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "当前用户部门树")
    public Result<Organization> tree() {
        String organizetionId = HeardersUtils.getOrganizetionId();
        // 获取当前部门
        Organization organization = organizationService.getById(organizetionId);
        if (organization == null) {
            return Result.error("当前部门不存在！");
        }
        organizationService.buildTree(organization);
        return Result.success(organization);
    }

    @PostMapping("insert")
    @ApiOperationSupport(order = 2, ignoreParameters = "children")
    @SaCheckPermission("system:organization:insert")
    @Operation(summary = "新增信息", description = "权限: system:organization:insert<br>")
    public Result<Boolean> insert(@RequestBody @Validated Organization organization) {
        if (organizationService.save(organization)) {
            RedisKey key = new RedisKey(KeyType.TIME,"organization*");
            RedisUtils.build().dels(key);
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("update")
    @ApiOperationSupport(order = 3, ignoreParameters = "children")
    @SaCheckPermission("system:organization:update")
    @Operation(summary = "修改信息", description = "权限: system:organization:update<br>")
    public Result<Boolean> update(@RequestBody @Validated Organization organization) {
        if (organizationService.updateById(organization)) {
            RedisKey key = new RedisKey(KeyType.TIME,"organization*");
            RedisUtils.build().dels(key);
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("info/{id}")
    @ApiOperationSupport(order = 4)
    @Operation(summary = "根据id获取信息", parameters = {
            @Parameter(name = "id", description = "业务主键", required = true, in = ParameterIn.PATH)
    })
    public Result<Organization> getInfo(@PathVariable String id) {
        return Result.success(organizationService.getById(id));
    }

    @GetMapping("remove/{id}")
    @ApiOperationSupport(order = 5)
    @SaCheckPermission("system:organization:update")

    @Operation(summary = "根据id删除", description = "权限: system:organization:update<br><font style='color: #1890FF'>假删除，会进入回收站</font>", parameters = {
            @Parameter(name = "id", description = "业务主键", required = true, in = ParameterIn.PATH)
    }, ignoreJsonView = true)
    public Result<Boolean> removeById(@PathVariable String id) {
        if (organizationService.update(Wrappers.<Organization>lambdaUpdate().set(Organization::getDel, 1).eq(Organization::getId, id))) {
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("delQuery")
    @ApiOperationSupport(order = 6)
    @SaCheckPermission({"system:organization:select", "system:organization:delete"})
    @Operation(summary = "回收站", description = "权限: <br>同时拥有system:organization:select、system:organization:delete")
    public Result<List<Organization>> delQuery() {
        List<Organization> result = organizationService.delPage();
        return Result.success(result);
    }

    @PostMapping("recovery")
    @ApiOperationSupport(order = 7)
    @SaCheckPermission({"system:organization:update", "system:organization:delete"})
    @Operation(summary = "根据id恢复删除的数据", description = "权限: <br>同时拥有system:organization:update、system:organization:delete")
    public Result<Boolean> recovery(@RequestBody @Validated BatchId batchId) {
        if (organizationService.recoveryByBatchId(batchId.getId())) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("delete")
    @ApiOperationSupport(order = 8)
    @SaCheckPermission("system:organization:delete")
    @Operation(summary = "根据id彻底删除", description = "权限: system:organization:delete<br><font style='color: red'>真删除，数据无法恢复</font>")
    public Result<Boolean> delete(@RequestBody @Validated BatchId batchId) {
        if (organizationService.deleteByBatchId(batchId.getId())) {
            return Result.success();
        }
        return Result.error();
    }
}